/*
function Computer(name, position){
	Player.call(this, name, position, false);
	console.log("Computer created");
}

Computer.prototype.play = function(){
	console.log("The computer player plays his cards");
}*/

var Computer = Player.extend({
	init: function(parent,index,name, x,y, position){
		this._super(parent,index,name, x,y, position, false);
		console.log("Computer created");
	},
	play: function(){
	
		console.log("******************** " + this.name + "***********************");
		if (this.done) {
			console.log(this.name + "is done");
		}
		else {
			if (this.hand.length == 0) {
				this.noCardsLeft = true;
			}
			else {

				if (typeof this.game.lastHand[0] === 'undefined') {
					for (var i=0;i<this.hand.length;i++) {
						if (this.hand[i].number == this.hand[0].number) {
							this.tempHand.push(this.hand[i]);
						}
					}
					
					for (var i=0;i<this.hand.length;i++) {
						for (var j=0;j<this.tempHand.length;j++) {
							if (this.hand[i].name == this.tempHand[j].name) {					
								this.hand.splice(i,1);
							}
						}
					}
				}
				else {
					var done = false;
					this.tempHand = [];
					for (var i=0;i<this.hand.length;i++) {
						if (done)
							break;
						if (this.game.highestCard(this.hand[i],this.game.lastHand[0])) {
							
							var number = this.hand[i].number;
							var j = i;
							this.tempHand = []
							while (this.hand[j].number == number) {
								if (this.tempHand.length != this.game.lastHand.length) {
									this.tempHand.push(this.hand[i]);
								}
								else {
									j=0;
									break;
								}
								j++;
								if (typeof this.hand[j] === 'undefined') 
									break;
							}
						}
							
						if (this.tempHand.length == this.game.lastHand.length) {
							for (var i=0;i<this.hand.length;i++) {
								for (var j=0;j<this.tempHand.length;j++) {
									if (this.hand[i].name == this.tempHand[j].name) {
										this.hand.splice(i,1);
									}
								}
							}
							done = true;
						}						
						else {
							this.tempHand = []; 
						}
					}			
				}
			}
	
			if (this.tempHand.length == 0) {
				this.pass = true;
				console.log(this.name + " passes");
			}
			else
				this.ready = true;
			
			for (var i=0;i<this.tempHand.length;i++) {
				console.log(this.name + " plays " + this.tempHand[i].number + " of " + this.tempHand[i].suit);
			}
		}
	}
})
